package com.dlc.beijingrfid.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.dlc.commonlibrary.adapter.BaseRecyclerAdapter;
import com.dlc.commonlibrary.utils.LogPlus;
import com.dlc.commonlibrary.utils.ToastUtil;
import com.dlc.commonlibrary.widget.popup.CommonPopupList;
import com.dlc.beijingrfid.R;
import com.dlc.beijingrfid.activity.OrderDetailActivity;
import com.dlc.beijingrfid.adapter.WashOrderAdapter;
import com.dlc.beijingrfid.adapter.decoration.RecyclerSpace;
import com.dlc.beijingrfid.base.BaseFragment;
import com.dlc.beijingrfid.base.Constants;
import com.dlc.beijingrfid.net.bean.HotelBean;
import com.dlc.beijingrfid.net.bean.WashOrderBean;
import com.dlc.beijingrfid.net.bean.WashOrderDetailBean;
import com.dlc.beijingrfid.net.protocol.OrderProto;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class WashOrderFragment extends BaseFragment {

    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_day_order_t)
    TextView tv_day_order_t;
    @BindView(R.id.tv_day_wash_money_t)
    TextView tv_day_wash_money_t;
    @BindView(R.id.tv_day_order_value)
    TextView tv_day_order_value;
    @BindView(R.id.tv_day_wash_money_value)
    TextView tv_day_wash_money_value;
    @BindView(R.id.tv_warehouse) TextView mTvWarehouse;
    @BindView(R.id.drop_warehouse) FrameLayout mDropWarehouse;

    private CommonPopupList mWarehousePopup;
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private int mParam1;
    private int mParam2;
    private WashOrderAdapter<WashOrderBean.DataBean.ListBean> adapter;
    String hotel_id = null;
    List<HotelBean.DataBean> hotelData;

    public WashOrderFragment() {
        // Required empty public constructor
    }

    public static WashOrderFragment newInstance(int param1, int param2) {
        WashOrderFragment fragment = new WashOrderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, param1);
        args.putInt(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getInt(ARG_PARAM1);
            mParam2 = getArguments().getInt(ARG_PARAM2);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_wash_order;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initRecycleView();
        initPopupLists();
    }

    private void initView() {
        String dayString = getDayString();
        switch (mParam2){
            case Constants.START_TYPE.WASH_ORDER:
                tv_day_order_t.setText(dayString+"订单量");
                tv_day_wash_money_t.setText(dayString+"洗涤总费用");
                break;
            case Constants.START_TYPE.LEASE_ORDER:
                tv_day_order_t.setText(dayString+"租赁订单量");
                tv_day_wash_money_t.setText(dayString+"租赁总费用");
                break;
        }
    }

    private String getDayString(){
        String days = "";
        switch (mParam1){
            case 0:
                days = "日";
                break;
            case 1:
                days = "月";
                break;
            case 2:
                days = "年";
                break;
        }
        return days;
    }
    private void initRecycleView() {
        adapter = new WashOrderAdapter<>(mParam2);

        adapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, BaseRecyclerAdapter.CommonHolder holder,
                                    int position) {
                WashOrderBean.DataBean.ListBean bean = adapter.getItem(position);
                if(mParam2 == Constants.START_TYPE.WASH_ORDER){
                    startActivity(new Intent(getActivity(),OrderDetailActivity.class).putExtra("type", Constants.START_TYPE.WASH_ORDER).putExtra("oid",bean.getId()));
                }else{
                    startActivity(new Intent(getActivity(),OrderDetailActivity.class).putExtra("type", Constants.START_TYPE.LEASE_ORDER).putExtra("oid",bean.getId()));
                }
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.addItemDecoration(new RecyclerSpace(getResources(), 0, R.dimen.normal_24dp));
        mRecyclerView.setAdapter(adapter);

        initData();
    }

    /**
     * 初始化数据
     */
    private void initData(){

        showWaitingDialog("...",true);
        switch (mParam2){
            case Constants.START_TYPE.WASH_ORDER:
                OrderProto.get().orderWash("1","100",mParam1+1,hotel_id).
                        subscribeOn(Schedulers.io()).
                        observeOn(AndroidSchedulers.mainThread()).
                        subscribe(new Observer<WashOrderBean>() {
                            @Override
                            public void onCompleted() {
                                dismissWaitingDialog();
                            }

                            @Override
                            public void onError(Throwable e) {
                                LogPlus.e(e.getMessage());
                                dismissWaitingDialog();
                            }

                            @Override
                            public void onNext(WashOrderBean data) {
                                adapter.setNewData(data.getData().getList());
                                tv_day_order_value.setText(String.valueOf(data.getData().getTotal()));
                                tv_day_wash_money_value.setText("￥"+String.valueOf(data.getData().getTotal_amount()));

                            }
                        });
                break;
            case Constants.START_TYPE.LEASE_ORDER:
                OrderProto.get().orderRent("1","100",mParam1+1,hotel_id).
                        subscribeOn(Schedulers.io()).
                        observeOn(AndroidSchedulers.mainThread()).
                        subscribe(new Observer<WashOrderBean>() {
                            @Override
                            public void onCompleted() {
                                dismissWaitingDialog();
                            }

                            @Override
                            public void onError(Throwable e) {
                                LogPlus.e(e.getMessage());
                                dismissWaitingDialog();
                            }

                            @Override
                            public void onNext(WashOrderBean data) {
                                adapter.setNewData(data.getData().getList());
                                tv_day_order_value.setText(String.valueOf(data.getData().getTotal()));
                                tv_day_wash_money_value.setText("￥"+String.valueOf(data.getData().getTotal_amount()));
                            }
                        });
                break;
        }
    }

    /**
     * 初始化下拉菜单（没设置数据）
     */
    private void initPopupLists() {
        mWarehousePopup = CommonPopupList.newBuilder(getActivity())
                .setWidthEquals(mDropWarehouse)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                      //  ToastUtil.showOne(getActivity(), "position=" + position);
                        // TODO: 2017/7/20
                        mTvWarehouse.setText(item);
                        if(hotelData != null){
                          hotel_id =  hotelData.get(position).getId();
                          if(hotel_id != null){
                                initData();
                          }
                        }
                    }
                })
                .build();

        getPopupListData();
    }

    private void getPopupListData() {
        OrderProto.get().getHotel().
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Observer<HotelBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogPlus.e(e.getMessage());
                    }

                    @Override
                    public void onNext(HotelBean hotelBean) {
                        if(hotelBean != null && hotelBean.getData() != null){
                            List<String> datas = new ArrayList<>();
                            hotelData = hotelBean.getData();
                            for(HotelBean.DataBean bean : hotelBean.getData()){
                                datas.add(bean.getTitle());
                            }
                            mWarehousePopup.setData(datas);
                        }
                    }
                });
    }

    @OnClick({ R.id.drop_warehouse})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.drop_warehouse:
                mWarehousePopup.showAsDropDown(view);
                break;
        }
    }
}
